#!/sw/bin/perl

package msqIRC::Interface::GUI;

use strict;
use warnings;

require exporter;

use Tk;
use Tk::ROText;
use Tk::Dialog;
use Tk::HList;
use Tk::NoteBook;


our @ISA = qw(Exporter);
our @EXPORT_OK = qw(main_loop generate_gui hlist_add hlist_remove tab_construct tab_destruct tab_write);

our $VERSION = 0.01;



sub generate_gui;
sub initialize_dialog;
sub initialize_display;
sub initialize_frame;
sub initialize_list;
sub initialize_input;
sub initialize_menu;


my %dialog;
my %frame;
my %list;
my %menu;
my %tab;
my %text;

my $book;
my $hlist;
my $input;
my $menu;
my $window;


sub generate_gui {
  $window = new MainWindow( -title => 'msqirc' );
  $menu = $window->Menu;
  
  $window->configure( -menu => $menu );
  &initialize_menu;
  &initialize_dialog;
  &initialize_frame;
  &initialize_list;
  &initialize_display;
  &initialize_input;
  $input->focus;
  
}
  
sub main_loop {
  MainLoop;
}

sub initialize_dialog {
	
	
	$dialog{ about } = $window->Dialog( 
                                     -title => 'about', 
                                     -text => "Version 0.01",
                                     -bitmap => 'info',
                                     -buttons => ['Dismiss'],
                                    );
	$dialog{ about }->configure( -wraplength => '6i' );
	
	$dialog{ usage } = $window->Dialog( 
                                     -title => 'Usage',
                                     -text => "Not useable right now." ,
                                     -buttons => ['Dismiss'],
                                    );
}

sub initialize_display {
	
	$book = $frame{ right }->NoteBook()->pack;
	&tab_construct( 'diagnostic' );
	
	

}
sub initialize_frame {
	$frame{ super } = $window->Frame()->pack(
                                           -side   => 'top',
                                           -fill   => 'both',
                                           -expand => 1
                                          );
	$frame{ left } = $frame{ super }->Frame()->pack(
                                                  -side   => 'left',
                                                  -fill   => 'y',
                                                  -expand => 1
                                                 );
	$frame{ right } = $frame{ super }->pack(
                                          -side   => 'right',
  }o
                                          -fill   => 'both'
                                         );
}

sub initialize_list {
	$hlist = $frame{ left }->Scrolled(
                                    'HList',
                                    -width       => 20,
                                    -itemtype    => 'text',
                                    -separator   => '-',
                                    -selectmode  => 'single',
                                    -scrollbars  => 'osoe',
                                    -command     => sub { $book->raise( shift( @{ $hlist->info( 'selection' )})) }
                                   )->pack(-side   =>'top',
                                           -fill   => 'y',
                                           -expand => 1,
                                          );
	&hlist_add( 'diagnostic' ); 
}

sub initialize_input {
	$input = $frame{ right }->Entry()->pack(
                                          -side   => 'left',
                                          -fill   => 'both',
                                          -expand => 1,
                                          -pady   => 2
                                         );
	$input->bind("<Return>", \&parse_input );
	$input->bind("<Control-Return>", \&tab_complete );
	$input->bind("<Tab>", \&tab_complete );
	
}

sub initialize_menu {
	
	$menu{ file } = $menu->cascade( 
                                 -label => '~File',
                                 -tearoff => 0,
                                );
	$menu{ file }->command(
                         -label => 'Quit',
                         -command => sub { exit },
                         -underline => 0,
                        );
	
	$menu{ edit } = $menu->cascade(
                                 -label => '~Edit',
                                 -tearoff => 0,
                                );
	
	$menu{ help } = $menu-> cascade(
                                  -label => '~Help',
                                  -tearoff => 0,
                                 );
	$menu{ help }->command(
                         -label => 'About',
                         -command => sub { print 'foo\n' },
                         -underline => 0
                        );
}


sub hlist_add;
sub hlist_remove;
sub tab_construct;
sub tab_destruct;
sub tab_write;


sub hlist_add {
	
	my $input = shift;
	my ($host, $chan) = split( /-/, $input, 2);
  
  warn "No host provided\n" if not $host;
  $chan = "NO_CHANNEL" if not $chan;
  
	my $text = $host;
	
	
	if ( $chan ne "NO_CHANNEL" ) {
		$host = "$host-$chan";
		$text = $chan;
	}
  
  print "\n\t$host--::--$chan--::--$text\n";
	$hlist->add( $host, -text => $text );
}

sub hlist_remove {
	my $target = shift || warn "No target provided\n";
	
	$hlist->delete( 'entry', $target)
}

sub tab_construct {
	
	my $name = shift || warn "No name provided\n", return;
	
  $name =~ m/([^-]+)-?(.+)?/;
  my $label = $2 ? $2 : $name;
  
  
	$tab{ $name } = $book->add( $name, -label => $label );
	$text{ $name } = $tab{ $name }->Scrolled( "ROText",
                                            -width => 80,
                                            -height => 30,
                                            -wrap => 'word',
                                            -bg => 'white',
                                            -relief => 'sunken',
                                            -scrollbars => 'oe' 
                                          )->pack(-side   => 'top',
                                                  -fill   => 'both',
                                                  -expand => 1);
	
}

sub tab_destruct {
  
  my $target = shift || warn "No target provided", return;
  
  $book->delete( $target );
}

sub tab_write {
	
	my ($target, $message) = @_;
	#  my $message = shift;
	#  my $name = $book->raised;
	my $text = $text{ $target } || warn "Incorrect data\n", return;
  
  print "\t$target-*-$message\n";
  
	$text->insert( 'end', $message );
  $text->see( 'end' );
}


1;




